package cn.big.bigd.biocode.specification;


import cn.big.bigd.biocode.dto.SearchDTO;
import cn.big.bigd.biocode.model.Tool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 *@Author wangfan
 *@Date 2018/1/16 16:16
 */

public class CustomerSpecifications {

private static final Logger logger = LoggerFactory.getLogger(CustomerSpecifications.class);



    public static Specification<Tool> searchSpec(SearchDTO search) {

        return (root, query, cb) -> {

            query.distinct(true);
            Predicate predicate = cb.conjunction();
            List<Expression<Boolean>> expressions = predicate.getExpressions();

            if (null!=search.getName()&&!"".equals(search.getName())) {

                expressions.add(cb.like(root.get("name"), "%"+search.getName()+"%"));
            }



         if(null!=search.getTool_type_id()&&search.getTool_type_id().length>0){

                expressions.add(root.join("toolType").get("id").in(search.getTool_type_id()));

            }

            if(null!=search.getTech_id()&&search.getTech_id().length>0){

                expressions.add(root.join("teches").get("id").in(search.getTech_id()));

            }
            if(null!=search.getInput_data_type_id()&&search.getInput_data_type_id().length>0){

                expressions.add(root.join("dataTypes").get("id").in(search.getInput_data_type_id()));

            }
            if(null!=search.getPlatform_id()&&search.getPlatform_id().length>0){

                expressions.add(root.join("platforms").get("id").in(search.getPlatform_id()));

            }
            if(null!=search.getUser_interface_id()&&search.getUser_interface_id().length>0){

                expressions.add(root.join("userInterfaces").get("id").in(search.getUser_interface_id()));

            }

            if(null!=search.getCategory_id()&&search.getCategory_id().length>0){

                expressions.add(root.join("categories").get("id").in(search.getCategory_id()));

            }

   /*           if(!search.getChrom().isEmpty()){
                expressions.add(cb.equal(root.get("chrom"), search.getChrom()));
            }
            if(0!=search.getStartposition()){
                expressions.add(cb.greaterThan(root.get("position"), search.getStartposition()));
            }
            if(0!=search.getEndposition()){
                expressions.add(cb.lessThan(root.get("position"), search.getEndposition()));
            }

            if(!search.getGene().isEmpty()){


                Predicate p1 = cb.equal(root.join("acList", JoinType.LEFT).get("geneSymbol").as(String.class),search.getGene());

                Predicate p2 = cb.equal(root.join("aoList", JoinType.LEFT).get("gene").as(String.class),search.getGene());

                Predicate p3 = cb.equal(root.join("apList", JoinType.LEFT).get("gene").as(String.class),search.getGene());

                expressions.add(cb.or(p1,p2,p3));
            }


            if (null!=search.getClinicalSignficances()&&search.getClinicalSignficances().length > 0){
                expressions.add(root.join("acList").get("clinicalSignficance").in(search.getClinicalSignficances()));


            }


            if (null!=search.getPhenotypeDescriptions()&&search.getPhenotypeDescriptions().length > 0){

                List<Predicate> predicates = new ArrayList<>();

                for(int i=0;i<search.getPhenotypeDescriptions().length ;i++){

                    Predicate p1= cb.or(cb.like(root.join("acList", JoinType.LEFT).get("phenotypeDescription").as(String.class),search.getPhenotypeDescriptions()[i]+"%"));

                    Predicate p2 =  cb.or(cb.like(root.join("acList", JoinType.LEFT).get("phenotypeDescription").as(String.class),"%;"+search.getPhenotypeDescriptions()[i]+"%"));
                    predicates.add(cb.or(p1,p2));

                }

                for (Predicate p:predicates){
                    cb.or(predicate);
                }

//                expressions.add(cb.or(predicates.t));


            }*/





            return predicate;
        };
    }


}
